<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2025/4/16
  Time: 20:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("UTF-8");
%>
<html>
<head>

    <title>Title</title>
</head>
<body>
<%
    /**
     * 获取上一个页面传过来的参数
     */
    String name = request.getParameter("name");
    String des = request.getParameter("des");
    String price = request.getParameter("price");
    String img = request.getParameter("img");
    /**
     * 以下代码为连接数据库代码
     */
    String dbname = "goods";//数据库名称
    String username = "root";//数据库用户名
    String password = "1234";//数据库密码
    String url = "jdbc:mysql://localhost:3306/" + dbname;
//    Class.forName("com.mysql.jdbc.Driver");//mysql 5.7一下的版本
    Class.forName("com.mysql.cj.jdbc.Driver");//mysql 8.0
    Connection connection = DriverManager.getConnection(url, username, password);
    PreparedStatement preparedStatement =
            connection.prepareStatement("insert into goods(name,des,img,price) values(?,?,?,?) ");
    preparedStatement.setString(1, name);
    preparedStatement.setString(2, des);
    preparedStatement.setString(3, img);
    preparedStatement.setString(4, price);
    //执行新增语句
    int resulet = preparedStatement.executeUpdate();
    if (resulet > 0) {//添加成功
        response.sendRedirect("search.jsp");//回到搜索页面
    }
%>
</body>
</html>
